package com.baiyi.homeui.hshomeui.util;

import android.app.Activity;
import android.app.AlarmManager;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;

public class Util {


    public static int getScreenBrightness(Activity activity) {
        ContentResolver contentResolver = activity.getContentResolver();
        int defVal = 125;
        return Settings.System.getInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, defVal);
    }

    public static void changeAppBrightness(int brightness, Activity activity) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        SLog.d("changeAppBrightness","changeAppBrightness---"+brightness);
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = (brightness <= 0 ? 0 : brightness) / 255f;
        }
        window.setAttributes(lp);
        saveBrightness(activity.getContentResolver(),brightness);
    }

    public static void saveBrightness(ContentResolver resolver, int brightness) {
        //改变系统的亮度值
        // 这里需要权限android.permission.WRITE_SETTINGS
        // 设置为手动调节模式
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        //保存到系统中
        Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
        resolver.notifyChange(uri, null);
    }

    public static int[][] convertToIntArray(ArrayList<ArrayList<Integer>> list) {
        // 获取外部ArrayList的大小
        int rows = list.size();

        // 根据外部ArrayList的大小创建二维数组
        int[][] result = new int[rows][];

        for (int i = 0; i < rows; i++) {
            ArrayList<Integer> innerList = list.get(i);

            // 获取当前内部ArrayList的大小
            int cols = innerList.size();

            // 在结果数组中创建新的一维数组
            result[i] = new int[cols];

            // 将内部ArrayList的值复制到结果数组中
            for (int j = 0; j < cols; j++) {
                result[i][j] = innerList.get(j);
            }
        }

        return result;
    }

    public static void setAndroidSystemTime(Context mContext,long time) {
        Log.d("setAndroidSystemTime", "时间===" + time);
        ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE))
                .setTime(time);
    }
}
